﻿
using System;
using System.Collections.Generic;
using System.Data;

using CoreLab.Oracle; //using Devart.Data.Oracle; //using Oracle.DataAccess.Client;
//using Oracle.DataAccess.Types;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Oracle;

namespace DBQuery
{
    /// <summary>
    /// Summary description for NewsFeedTable
    /// </summary>
    public static class NewsFeedTable
    {
        public static void SaveNewsFeed(NewsFeedData nfData)
        {
            DBLib dbl = new DBLib();
            SaveNewsFeed(dbl, nfData);
            
        }
        public static void SaveNewsFeed(DBLib dbl, NewsFeedData nfData)
        {
            List<OracleParameter> lsParams = new List<OracleParameter>();

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "out_new_id";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Output;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "out_new_date";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Date;
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Output;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_TITLE";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = nfData.sxTitle;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_CONTENT";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = nfData.sxContent;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_POSTING_USER_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = nfData.sxUserId_Poster;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_POSTED_USER_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = nfData.sxUserId_Target;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_CUSTOMER_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            if (nfData.iCustomerId < 0)
            {
                lsParams[lsParams.Count - 1].Value = DBNull.Value;
            }
            else
            {
                lsParams[lsParams.Count - 1].Value = nfData.iCustomerId;
            }

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_FEED_SOURCE_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = nfData.iSourceId;

            String sxQuery = "PR_INSERT_T_NEWS_FEED";

            dbl.ExecuteProcedure(sxQuery, lsParams.ToArray());

            nfData.ID = Convert.ToInt32(lsParams[0].Value);
            nfData.dtmDate = Convert.ToDateTime(lsParams[1].Value);
        }

        public static int SaveNewsFeedData(int iNewsFeedDataTypeId, int iNewsFeedId, String sxValue)
        {
            DBLib dbl = new DBLib();
            return SaveNewsFeedData(dbl, iNewsFeedDataTypeId, iNewsFeedId, sxValue);
            
        }
        public static int SaveNewsFeedData(DBLib dbl, int iNewsFeedDataTypeId, int iNewsFeedId, String sxValue)
        {
            List<OracleParameter> lsParams = new List<OracleParameter>();

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "out_new_id";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Output;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_VALUE";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = sxValue;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_T_NEWS_FEED_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = iNewsFeedId;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_P_NEWS_FEED_DATA_TYPE_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = iNewsFeedDataTypeId;

            String sxQuery = "PR_INSERT_T_NEWS_FEED_DATA";

            dbl.ExecuteProcedure(sxQuery, lsParams.ToArray());

            return Convert.ToInt32(lsParams[0].Value);
        }

        public static int SaveNewsFeedComment(CommentData cmData)
        {
            DBLib dbl = new DBLib();
            return SaveNewsFeedComment(dbl, cmData);
            
        }
        public static int SaveNewsFeedComment(DBLib dbl, CommentData cmData)
        {
            List<OracleParameter> lsParams = new List<OracleParameter>();

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "out_new_id";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Output;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "out_new_date";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Date;
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Output;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "out_notif_id";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Output;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_T_NEWS_FEED_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = cmData.NewsFeedID; 
            
            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_VALUE";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = cmData.sxContent;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_ICP_USER_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = cmData.sxUserId;

            String sxQuery = "PR_INSERT_T_FEED_COMMENT";

            dbl.ExecuteProcedure(sxQuery, lsParams.ToArray());

            //cmData.ID = Convert.ToInt32(lsParams[0].Value);
            cmData.ID = Convert.ToInt32(lsParams[0].Value);
            cmData.dtmDate = Convert.ToDateTime(lsParams[1].Value);

            return Convert.ToInt32(lsParams[2].Value);
        }

        public static void DeleteNewsFeed(int iNewsFeedId) 
        {
            DeleteNewsFeed(new DBLib(), iNewsFeedId);
        }
        public static void DeleteNewsFeed(DBLib dbl, int iNewsFeedId) 
        {
            OracleParameter prm = new OracleParameter();

            prm.ParameterName = "in_T_NEWS_FEED_ID";
            prm.OracleDbType = OracleDbType.Number;
            prm.Value = iNewsFeedId;

            String sxQuery = "PR_DELETE_T_NEWS_FEED";

            dbl.ExecuteProcedure(sxQuery, prm);
        }

        public static void BlockNewsFeed_FromUser(String sxUID)
        {
            BlockNewsFeed_FromUser(new DBLib(), sxUID);
        }
        public static void BlockNewsFeed_FromUser(DBLib dbl, String sxUID)
        {
            OracleParameter prm = new OracleParameter();
            prm.ParameterName = "in_ICP_USER_ID";
            prm.OracleDbType = OracleDbType.VarChar;
            prm.Value = ICPUser.ID;

            OracleParameter prm2 = new OracleParameter();
            prm2.ParameterName = "in_BLOCKED_USER_ID";
            prm2.OracleDbType = OracleDbType.VarChar;
            prm2.Value = sxUID;

            String sxQuery = "PR_BLOCK_USER_FEED";

            dbl.ExecuteProcedure(sxQuery, prm, prm2);
        }

        public static void UnblockNewsFeed_FromUser(String sxUID)
        {
            UnblockNewsFeed_FromUser(new DBLib(), sxUID);
        }
        public static void UnblockNewsFeed_FromUser(DBLib dbl, String sxUID)
        {
            OracleParameter prm = new OracleParameter();
            prm.ParameterName = "in_ICP_USER_ID";
            prm.OracleDbType = OracleDbType.VarChar;
            prm.Value = ICPUser.ID;

            OracleParameter prm2 = new OracleParameter();
            prm2.ParameterName = "in_BLOCKED_USER_ID";
            prm2.OracleDbType = OracleDbType.VarChar;
            prm2.Value = sxUID;

            String sxQuery = "PR_UNBLOCK_USER_FEED";

            dbl.ExecuteProcedure(sxQuery, prm, prm2);
        }

        public static void BlockNewsFeed_FromCustomer(int iCustomerId)
        {
            BlockNewsFeed_FromCustomer(new DBLib(), iCustomerId);
        }
        public static void BlockNewsFeed_FromCustomer(DBLib dbl, int iCustomerId)
        {
            OracleParameter prm = new OracleParameter();
            prm.ParameterName = "in_ICP_USER_ID";
            prm.OracleDbType = OracleDbType.VarChar;
            prm.Value = ICPUser.ID;

            OracleParameter prm2 = new OracleParameter();
            prm2.ParameterName = "in_CUSTOMER_ID";
            prm2.OracleDbType = OracleDbType.Number;
            prm2.Value = iCustomerId;

            String sxQuery = "PR_BLOCK_CUSTOMER_FEED";

            dbl.ExecuteProcedure(sxQuery, prm, prm2);
        }

        public static void UnblockNewsFeed_FromCustomer(int iCustomerId)
        {
            UnblockNewsFeed_FromCustomer(new DBLib(), iCustomerId);
        }
        public static void UnblockNewsFeed_FromCustomer(DBLib dbl, int iCustomerId)
        {
            OracleParameter prm = new OracleParameter();
            prm.ParameterName = "in_ICP_USER_ID";
            prm.OracleDbType = OracleDbType.VarChar;
            prm.Value = ICPUser.ID;

            OracleParameter prm2 = new OracleParameter();
            prm2.ParameterName = "in_CUSTOMER_ID";
            prm2.OracleDbType = OracleDbType.Number;
            prm2.Value = iCustomerId;

            String sxQuery = "PR_UNBLOCK_CUSTOMER_FEED";

            dbl.ExecuteProcedure(sxQuery, prm, prm2);
        }
    }
}